package me.edzh.spring.bootstrap.utils.security;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.yhb.utils.EncryptConfig;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

import static com.yhb.utils.Constants.RSA_SEGMENT;

public class RSAUtils {

    private static final PublicKey publicKey = getPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiFuIN7lkhZuORKwYCda4IKxHI1Qm227huM1GcP+evWdrzx7aRdqeudsQo01wWzRbi11Pu1kPJZJeF0xrNthAWTwqlzG8vWRzTzh7NVFAZvdMye0b88acXOxGnhndPbS3zN/IOmyqRJBmc0AohfNejoq9Xnh/D/jxzuz0CVlgK6K9Z+fz/ugi8oQTMvgUaRKuWsxbCLQPg+MOKJUQjEr42dEl9LldNEDGaA7tPqlE8To0N9XAlzI+8MwntflrUCixxBPMYEgm6ksfGA6AMKwwhfJLlJ2icW1dW6WLOq1VKxEz3076p97BjrVTANooK4joHR4O5Swh4rrUFUismfAzUwIDAQAB");
    private static final PrivateKey privateKey = new EncryptConfig().privateKey();

    public static void main(String[] args) {
        // 生成RSA key
//        try {
//            KeyPair keyPair = genKeyPair(2048);
//            System.out.println("public key:\n" + new String(Base64.getEncoder().encode(keyPair.getPublic().getEncoded())));
//            System.out.println("private key:\n" + new String(Base64.getEncoder().encode(keyPair.getPrivate().getEncoded())));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        // 加解密测试
        try {
            Map a = new HashMap<String, String>();

            String testString = "dfasdlfkasjfdkjasjasdfasdfasdlfdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfkkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkdfasdlfkasjfdkjasjasdfasldfjskfjsdlfkldfjskfjsdlfk";
            byte[] toEn = testString.getBytes(StandardCharsets.UTF_8);
            byte[] content = encrypt(toEn, publicKey, RSA_SEGMENT);
            // 写入文件 用于mock
//            File f = new File("/Users/edwardzhou/Desktop/fuck1.out");
//            FileUtils.writeByteArrayToFile(f, content);
            byte[] de = decrypt(content, privateKey, RSA_SEGMENT);
            System.out.println(new String(de, StandardCharsets.UTF_8));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //生成密钥对
    public static KeyPair genKeyPair(int keyLength) throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(keyLength);
        return keyPairGenerator.generateKeyPair();
    }

    //公钥加密
    public static byte[] encrypt(byte[] content, PublicKey publicKey, int segmentSize) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Cipher cipher = Cipher.getInstance("RSA");//java默认"RSA"="RSA/ECB/PKCS1Padding"
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);


        return doFinal(content, cipher, segmentSize);
    }

    //私钥解密
    public static byte[] decrypt(byte[] content, PrivateKey privateKey, int segmentSize) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException  {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);


        return doFinal(content, cipher, segmentSize);
    }

    private static byte[] doFinal(byte[] content, Cipher cipher, int segmentSize) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int inputLen = content.length;
        int offSet = 0;
        byte[] cache;
        int i = 0;
        // 对数据分段解密
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > segmentSize) {
                cache = cipher.doFinal(content, offSet, segmentSize);
            } else {
                cache = cipher.doFinal(content, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * segmentSize;
        }
        byte[] data = out.toByteArray();
        try {
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    /**
     * 得到公钥
     *
     * @param key 密钥字符串（经过base64编码）
     * @throws Exception
     */
    public static PublicKey getPublicKey(String key) {
        try {
            byte[] keyBytes;
            keyBytes = decode(key);

            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            return keyFactory.generatePublic(keySpec);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 得到私钥
     *
     * @param key 密钥字符串（经过base64编码）
     * @throws Exception
     */
    public static PrivateKey getPrivateKey(String key) {
        try {
            byte[] keyBytes;
            keyBytes = decode(key);

            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
            return privateKey;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param bytes
     * @return
     */
    public static byte[] decode(final String bytes) {
        return Base64.decodeBase64(bytes);
    }

    /**
     * 二进制数据编码为BASE64字符串
     *
     * @param bytes
     * @return
     * @throws Exception
     */
    public static String encode(final byte[] bytes) {
        return Base64.encodeBase64String(bytes);
    }

}
